package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoSchoolArticleMaterialDao;
import com.sc.nft.entity.dto.DaoSchoolArticleMaterialSaveAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolMaterialArticlePageAdminDTO;
import com.sc.nft.entity.vo.ArticleMaterialVO;
import com.sc.nft.entity.vo.DaoSchoolMaterialArticlePageAdminVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoSchoolArticleMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 素材表与文章表关联表(DaoSchoolArticleMaterial)表服务实现类
 *
 * @author makejava
 * @since 2023-03-13 16:21:33
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoSchoolArticleMaterialServiceImpl implements DaoSchoolArticleMaterialService {

    private final DaoSchoolArticleMaterialDao daoSchoolArticleMaterialDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByArticleId(Long id) {
        return daoSchoolArticleMaterialDao.deleteByArticleId(id);
    }

    /**
     * 后台获取分页列表
     *
     * @param daoSchoolMaterialArticlePageAdminDTO
     * @return
     */
    @Override
    public Page<DaoSchoolMaterialArticlePageAdminVO> pageAdmin(DaoSchoolMaterialArticlePageAdminDTO daoSchoolMaterialArticlePageAdminDTO) {
        return daoSchoolArticleMaterialDao.pageAdmin(daoSchoolMaterialArticlePageAdminDTO);
    }

    /**
     * 后台获取分页列表
     *
     * @param daoSchoolMaterialArticlePageAdminDTO
     * @return
     */
    @Override
    public Page<DaoSchoolMaterialArticlePageAdminVO> pageNotAdmin(DaoSchoolMaterialArticlePageAdminDTO daoSchoolMaterialArticlePageAdminDTO) {
        return daoSchoolArticleMaterialDao.pageNotAdmin(daoSchoolMaterialArticlePageAdminDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return daoSchoolArticleMaterialDao.deleteById(id);
    }

    /**
     * 新增附件
     *
     * @param listInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdmin(List<DaoSchoolArticleMaterialSaveAdminDTO> listInfo) {
        if (CollUtil.isEmpty(listInfo)) {
            throw new GlobalRunTimeException("参数不能为空");
        }
        DaoSchoolArticleMaterialSaveAdminDTO daoSchoolArticleMaterialSaveAdminDTO = listInfo.get(0);
        Integer integer = daoSchoolArticleMaterialDao.selectByArticleIdSum(daoSchoolArticleMaterialSaveAdminDTO.getArticleId());
        if (integer + listInfo.size() > 6) {
            throw new GlobalRunTimeException("素材数量不能超过6个");
        }
        return daoSchoolArticleMaterialDao.saveAdmin(listInfo);
    }

    /**
     * 根据文章id获取关联数据
     *
     * @param id
     * @return
     */
    @Override
    public List<ArticleMaterialVO> selectByArticleId(Long id) {
        return daoSchoolArticleMaterialDao.selectByArticleId(id);
    }

}
